<?php

namespace metronic\widgets\colorpicker;

use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
use yii\web\AssetBundle;

/**
 * ColorPicker
 *
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
class ColorPicker extends InputWidget {

    public $clientOptions = [
        'letterCase' => 'lowercase',
        'position' => 'bottom left',
        'theme' => 'bootstrap'
    ];
    private $_assetBundle;
    private $_controls = [
        'hue',
        'saturation',
        'brightness',
        'wheel'
    ];
    public $inputType = 'text';

    /**
     * @inheritdoc
     */
    public function init() {
        parent::init();
        $this->defaultOptions();
        $this->registerAssetBundle();
        $this->registerScript();
    }

    /**
     * @inheritdoc
     */
    public function run() {
        if (isset($this->options['type'])) {
            $this->inputType = $this->options['type'];
            unset($this->options['type']);
        }
        if ($this->hasModel()) {
            return Html::activeInput($this->inputType, $this->model, $this->attribute, $this->options);
        }
        return Html::input($this->inputType, $this->name, $this->value, $this->options);
    }

    /**
     * Register assetBundle
     */
    protected function registerAssetBundle() {
        $this->_assetBundle = ColorPickerAsset::register($this->getView());
    }

    /**
     * @return AssetBundle
     */
    public function getAssetBundle() {
        if (!($this->_assetBundle instanceof AssetBundle)) {
            $this->registerAssetBundle();
        }
        return $this->_assetBundle;
    }

    /**
     * @return bool|string The path of assetBundle
     */
    public function getSourcePath() {
        return Yii::getAlias($this->getAssetBundle()->sourcePath);
    }

    /**
     * Sets default options
     */
    protected function defaultOptions() {
        if (isset($this->options['inline'])) {
            $this->clientOptions['inline'] = $this->options['inline'];
            unset($this->options['inline']);
        }

        if (isset($this->options['opacity'])) {
            $this->clientOptions['opacity'] = $this->options['opacity'];
            unset($this->options['opacity']);
        }

        if ($this->hasModel()) {
            $this->value = Html::getAttributeValue($this->model, $this->attribute);
        }

        if ($this->value) {
            $this->value = '#' . ltrim($this->value, '#');
        } else {
            $this->value = '';
        }

        if (!isset($this->clientOptions['defaultValue'])) {
            $this->clientOptions['defaultValue'] = $this->value;
        }

        if (!isset($this->clientOptions['control'])) {
            $this->clientOptions['control'] = array_shift($this->_controls);
        } elseif (!in_array($this->clientOptions['control'], $this->_controls)) {
            $this->clientOptions['control'] = array_shift($this->_controls);
        }

        Html::addCssClass($this->options, 'form-control');

        if (YII_DEBUG) {
            $this->clientOptions['change'] = "function(hex, opacity) {
                if (!hex) return;
                if (opacity) hex += ', ' + opacity;
                if (typeof console === 'object') {
                    console.log(hex);
                }
            }";
        }
    }

    /**
     * Register clients script to View
     */
    protected function registerScript() {
        $clientOptions = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : '';
        $this->getView()->registerJs("jQuery('#{$this->options['id']}').minicolors({$clientOptions});");
    }

}
